/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExCoViewFrame.java
 *
 * Created on 29/11/2011, 23:52:27
 */

package proj5labbd.graphics.exibicaoComercial;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

public class ExCoViewPanel extends OperationPanel {

    /** Creates new form ExCoViewFrame */
    public ExCoViewPanel(ExCoFrame frame, DBInterface dbInterface) {
        super(frame,dbInterface);
        initComponents();
        jbtView.addActionListener(new ExCoAction());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbView = new javax.swing.JTable();
        jpnInsert = new javax.swing.JPanel();
        jlbDate = new javax.swing.JLabel();
        jlbInicio = new javax.swing.JLabel();
        jlbTermino = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jftxtDate = new javax.swing.JFormattedTextField();
        jftxtIni = new javax.swing.JFormattedTextField();
        jftxtFim = new javax.swing.JFormattedTextField();
        jftxtCliente = new javax.swing.JFormattedTextField();
        jbtView = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jchkDate = new javax.swing.JCheckBox();
        jchkInicio = new javax.swing.JCheckBox();
        jchkFim = new javax.swing.JCheckBox();
        jchkCliente = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Visualização"));

        jtbView.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbView);

        jpnInsert.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros de visualização"));

        jlbDate.setText("Data de exibição");

        jlbInicio.setText("Horário de início");

        jlbTermino.setText("Horário de término");

        jLabel1.setText("Cliente");

        try {
            jftxtDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-##-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtDate.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jftxtDate.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        try {
            jftxtIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtIni.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        try {
            jftxtFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtFim.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        try {
            jftxtCliente.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jpnInsertLayout = new javax.swing.GroupLayout(jpnInsert);
        jpnInsert.setLayout(jpnInsertLayout);
        jpnInsertLayout.setHorizontalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jftxtCliente)
                    .addComponent(jftxtDate, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jlbDate)
                    .addComponent(jLabel1))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlbInicio)
                    .addComponent(jftxtIni, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbTermino)
                    .addComponent(jftxtFim, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jpnInsertLayout.setVerticalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbDate)
                    .addComponent(jlbInicio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jftxtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnInsertLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jftxtCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpnInsertLayout.createSequentialGroup()
                        .addComponent(jlbTermino)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jftxtFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        jbtView.setText("Visualizar");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a selecionar"));

        jchkDate.setText("Data de exibição");
        jchkDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchkDateActionPerformed(evt);
            }
        });

        jchkInicio.setText("Horário de início");

        jchkFim.setText("Horário de término");

        jchkCliente.setText("Cliente");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkDate)
                    .addComponent(jchkInicio)
                    .addComponent(jchkFim)
                    .addComponent(jchkCliente)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jchkDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkInicio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkFim)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkCliente)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE)
                        .addGap(12, 12, 12))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jpnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addComponent(jbtView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpnInsert, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtView))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jchkDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchkDateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jchkDateActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtView;
    private javax.swing.JCheckBox jchkCliente;
    private javax.swing.JCheckBox jchkDate;
    private javax.swing.JCheckBox jchkFim;
    private javax.swing.JCheckBox jchkInicio;
    private javax.swing.JFormattedTextField jftxtCliente;
    private javax.swing.JFormattedTextField jftxtDate;
    private javax.swing.JFormattedTextField jftxtFim;
    private javax.swing.JFormattedTextField jftxtIni;
    private javax.swing.JLabel jlbDate;
    private javax.swing.JLabel jlbInicio;
    private javax.swing.JLabel jlbTermino;
    private javax.swing.JPanel jpnInsert;
    private javax.swing.JTable jtbView;
    // End of variables declaration//GEN-END:variables

    class ExCoAction implements ActionListener {

        /*
         * Controla os eventos de visualização
         */
        public void actionPerformed(ActionEvent e) {
            
            if (e.getSource() == jbtView) {
                
                ArrayList<String> param = new ArrayList<String>();
                ArrayList<String> field = new ArrayList<String>();
                
                if (jftxtDate.getText().compareTo("  -  -    ") != 0) {
                    param.add("exibicaoComercial.dataExCo="+"TO_DATE('"+jftxtDate.getText()+"','DD-MM-YYYY')");
                }
                if (jftxtIni.getText().compareTo("  :  ") != 0) {
                    param.add("exibicaoComercial.horaInicioExCo='"+jftxtIni.getText()+"'");
                }
                if (jftxtFim.getText().compareTo("  :  ") != 0) {
                    param.add("exibicaoComercial.horaFimExCo='"+jftxtFim.getText()+"'");
                }
                if (jftxtCliente.getText().compareTo("  .   .   /    -  ") != 0) {
                    param.add("exibicaoComercial.cnpjCl='"+jftxtCliente.getText()+"'");
                }
                if (jchkCliente.isSelected()) {
                    field.add("exibicaoComercial.cnpjCl");
                }
                if (jchkDate.isSelected()) {
                    field.add("exibicaoComercial.dataExCo");
                }
                if (jchkFim.isSelected()) {
                    field.add("exibicaoComercial.horaInicioExCo");
                }
                if (jchkInicio.isSelected()) {
                    field.add("exibicaoComercial.horaFimExCo");
                }

                String paramArray[] = new String[param.size()];
                String fieldArray[] = new String[field.size()];

                param.toArray(paramArray);
                field.toArray(fieldArray);
                
                try {
                    String[][] s = dbInterface.view(fieldArray, paramArray, new String[]{"exibicaoComercial"});
                    if (fieldArray.length == 0) {
                        fieldArray = new String[6];
                        fieldArray[0] = "ID Comercial";
                        fieldArray[1] = "Data";
                        fieldArray[2] = "Hora Inicio";
                        fieldArray[3] = "Hora Fim";
                        fieldArray[4] = "Preco";
                        fieldArray[5] = "CNPJ Cliente";
                    }
                    jtbView.setModel( new DefaultTableModel(s,fieldArray));
                } catch (SQLException ex) {
                    frame.printException(ex);
                    JOptionPane.showMessageDialog(null, "Erro ao tentar visualizar os dados.");
                }
                
            }
            
        }
        
    }

}
